// http模块 
const http = require('http');
// fs模块
const fs = require('fs');
// url模块

const url = require('url');

// 创建一个服务
const server = http.createServer(function(req,res){
    // /html 
    // /list
    // /detail 

    // url.parse(req.url,true);
    // console.log(req.url);
    const {query,pathname} = url.parse(req.url,true);
    // url.parse(req.url,true); 返回的是个对象
    //获取对象 的内容   可以还用对象的解构  
    // console.log(query);
    // console.log(pathname);
    // localhost:8890/home
    if(pathname === '/home'){
        fs.readFile('./client/views/home.html','utf8',(err,data)=>{
            if(err){
                console.log(err);
            }else{
                res.end(data);
            }
        })
    }

    // localhost:8890/list 
    if(pathname === '/list'){
        fs.readFile('./client/views/list.html','utf8',(err,data)=>{
            if(err){
                console.log(err);
            }else{
                res.end(data);
            }
        })
    }
    // localhost:8890/detail
    if(pathname === '/detail'){

        fs.readFile('./client/views/detail.html','utf8',(err,data)=>{
            if(err){
                console.log(err);
            }else{
                res.end(data);
            }
        })
    }
    
})

// 开启服务

server.listen(8890,()=>{
    console.log('服务开启成功');
})